﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media.Effects;
using System.Windows;
using Microsoft.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace MP3Ganizer.Controls.Message
{
    public class MessageManager
    {
        private Dictionary<Control, Window> _busyControlsDictionary;

        public MessageManager()
        {
            _busyControlsDictionary = new Dictionary<Control, Window>();
        }

        private void BlurMainWindow(ContentControl ctrl)
        {
            ctrl.Effect = new BlurEffect() { Radius = 7, RenderingBias = RenderingBias.Performance };
        }

        private void UnBlurMainWindow(ContentControl ctrl)
        {
            ctrl.Effect = null;
        }

        public void ShowBusyIndicator(ContentControl ctrl)
        {
            if (_busyControlsDictionary.ContainsKey(ctrl))
                return;
            Popup indicator = Application.Current.MainWindow.FindName("PART_BusyIndicator") as Popup;
            if (indicator == null)
                return;
            if(ctrl is FrameworkElement)
            {
                var element = ctrl as FrameworkElement;
                indicator.PlacementTarget = element;
                indicator.Placement = PlacementMode.Center;
            }
            BlurMainWindow(ctrl);
            indicator.IsOpen = true;
        }

        public void HideBusyIndicator(ContentControl ctrl)
        {
            Popup indicator = Application.Current.MainWindow.FindName("PART_BusyIndicator") as Popup;
            if (indicator == null)
                return;
            UnBlurMainWindow(ctrl);
            indicator.IsOpen = false;
        }



        //public void ShowServiceError(IServiceResult result)
        //{
        //    throw new NotImplementedException();
        //}
    }
}
